package it.uniroma3.dia.ia.textcategorization.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.jsoup.Jsoup;

import it.uniroma3.dia.ia.textcategorization.model.Text;

/**
 * 
 * Utility class for markup removal.
 * 
 * @author Marco Liceti
 *
 */
public final class MarkupRemover {

	/**
	 * 
	 * Removes markup from the given Text instance.
	 * 
	 * @param page The Text instance to remove markup from.
	 * @return a newly created Text instance with markup removed.
	 */
	public static Text removeMarkup(Text page) {
		Text text = null;
		
		try {
			String pageContent = getContent(page);
			String textString = Jsoup.parse(pageContent).text();
			InputStream inputStream = getInputStream(textString);
			text = new Text(inputStream);
		} catch (Exception exception) {
			// In case of an exception,
			// simply return null
		}
		
		return text;
	}

	private static String getContent(Text page) throws IOException {
		String content = null;
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				page.asInputStream()));
		String line = reader.readLine();
		StringBuilder sb = new StringBuilder();
		while (line != null) {
			sb.append(line + "\n");
			line = reader.readLine();
		}
		content = sb.toString();
		
		return content;
	}

	private static InputStream getInputStream(String textString) {
		return new ByteArrayInputStream(textString.getBytes());
	}

}
